#!/bin/bash

############################################################################
#
# 远程执行该脚本，请在命令行中直接执行以下命令：
#
#   curl -sSL https://gitee.com/tay3223/biubiubiu/raw/master/centos7-install-containerd.sh | /bin/bash
#
#
############################################################################


# TODO：https://docs.docker.com/engine/install/centos/
# 卸载原来旧的docker
systemctl disable docker && systemctl stop docker
yum remove docker \
    docker-client \
    docker-client-latest \
    docker-common \
    docker-latest \
    docker-latest-logrotate \
    docker-logrotate \
    docker-engine


# TODO：https://docs.docker.com/engine/install/centos/
# 安装远程仓库源和一些依赖的工具
yum install -y yum-utils

#yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

# 安装一些特定的驱动，这是一些实际问题中遇到所需要的依赖（上面才是官方推荐的步骤）
yum install -y epel-release \
    device-mapper-persistent-data \
    python-pip \
    lvm2 \
    git \
    wget \
    curl


# 安装containerd
systemctl disable containerd && systemctl stop containerd

yum install -y containerd
containerd config default > /etc/containerd/config.toml

sed -i 's/registry.k8s.io/registry.aliyuncs.com\/google_containers/g' /etc/containerd/config.toml

sed -i 's/config_path = ""/config_path = "\/etc\/containerd\/certs.d"/g' /etc/containerd/config.toml
mkdir -p /etc/containerd/certs.d


#163加速地址
tee /etc/containerd/certs.d/hub-mirror.c.163.com <<\EOF
server = "https://docker.io"
[host."https://hub-mirror.c.163.com"]
  capabilities = ["pull", "resolve"]

EOF

#https://dockerproxy.com
tee /etc/containerd/certs.d/dockerproxy.com <<\EOF
server = "https://docker.io"
[host."https://dockerproxy.com"]
  capabilities = ["pull", "resolve"]

EOF

#南京大学
tee /etc/containerd/certs.d/docker.nju.edu.cn <<\EOF
server = "https://docker.io"
[host."https://docker.nju.edu.cn"]
  capabilities = ["pull", "resolve"]

EOF

#清华大学
tee /etc/containerd/certs.d/docker.mirrors.ustc.edu.cn <<\EOF
server = "https://docker.io"
[host."https://docker.mirrors.ustc.edu.cn"]
  capabilities = ["pull", "resolve"]

EOF

#腾讯云
tee /etc/containerd/certs.d/ccr.ccs.tencentyun.com <<\EOF
server = "https://docker.io"
[host."https://ccr.ccs.tencentyun.com"]
  capabilities = ["pull", "resolve"]

EOF

#daocloud云
tee /etc/containerd/certs.d/docker.m.daocloud.io <<\EOF
server = "https://docker.io"
[host."https://docker.m.daocloud.io"]
  capabilities = ["pull", "resolve"]

EOF

#百度云
tee /etc/containerd/certs.d/mirror.baidubce.com <<\EOF
server = "https://docker.io"
[host."https://mirror.baidubce.com"]
  capabilities = ["pull", "resolve"]

EOF

#私人加速地址
tee /etc/containerd/certs.d/mirror.aliyuncs.com <<\EOF
server = "https://docker.io"
[host."https://vpe7576e.mirror.aliyuncs.com"]
  capabilities = ["pull", "resolve"]
  skip_verify = true #跳过证书验证

EOF

#安装nerdctl
wget -P /tmp/ https://osoc.oss-cn-shanghai.aliyuncs.com/bao/containerd/nerdctl-1.4.0-linux-amd64.tar.gz
tar -zxvf /tmp/nerdctl-1.4.0-linux-amd64.tar.gz -C /usr/local/bin/

#安装 container network interface
wget -P /tmp/ https://osoc.oss-cn-shanghai.aliyuncs.com/bao/containerd/cni-plugins-linux-amd64-v1.3.0.tgz
mkdir -p /opt/cni/bin
tar -zxvf /tmp/cni-plugins-linux-amd64-v1.3.0.tgz -C /opt/cni/bin/


# 启动 && 开机启动
echo -e "\n设置开机启动："
systemctl daemon-reload
systemctl enable containerd && systemctl start containerd

cat <<\Tay

====================================
1.已设置为开机启动（√ ）
2.已经启动docker服务（√ ）
3.相关命令：
    systemctl daemon-reload
    systemctl status containerd
    systemctl restart containerd
    systemctl start containerd
    systemctl enable containerd
    systemctl disable containerd
====================================

Tay

VERSION=$(containerd -v)
echo -e "\n当前docker版本为\n\n：${VERSION} \n"
